﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guoc.Web.Biz;
using Guoc.DAL.Domain;
using Guoc.Web.Tool;
namespace Guoc.Web.Controllers
{
    public class MenuController : Controller
    {
        public ActionResult MenuIndex()
        {
            return View();
        }



        public JsonResult QueryMenu(int page, int rows)
        {

            return Json(new { total = 1, rows = 1 });
        }

        public JsonResult GetAllMenus()
        {
            Dictionary<string, object> json = MenuBiz.Instance.GetTreegridFormateMenu();

            return Json(json);
        }



        public JsonResult LoadUserMenu()
        {
            //if (Admin.Current == null)
            //{
            //    return RedirectToAction("Login", "Home");
            //}
            //加载用户菜单
            //string html= MenuBiz.Instance.GetRoleMenu(Admin.Current.MyRole.RoleID);
            List<Dictionary<string, object>> json = MenuBiz.Instance.GetRoleMenu("1");

            return Json(json);
        }

        public JsonResult MenuCreate(string id, string title, string url, int sequence, int isShow, string parentID)
        {
            try
            {
                Menu mu = new Menu()
                {
                    Title = title,
                    Sequence = sequence,
                    ParentID = parentID,
                    IsShow = 1,
                    CreateTime = DateTime.Now,
                    Url = url
                };

                MenuBiz.Instance.Create(mu);

                return Json(new { code = 1, message = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 0, message = ex.Message });
            }
        }

        public JsonResult MenuRootCreate(string id, string title, string url, int sequence, int isShow, string parentID)
        {
            try
            {
                Menu mu = new Menu()
                {
                    Title = title,
                    Sequence = sequence,
                    IsShow = isShow,
                    CreateTime = DateTime.Now,
                    Url = url
                };

                MenuBiz.Instance.CreateMenuRoot(mu);

                return Json(new { code = 1, message = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 0, message = ex.Message });
            }
        }

        public JsonResult MenuEdit(string id, string title, string url, int sequence, int isShow, string parentID)
        {
            try
            {
                Menu mu = new Menu()
                {
                    ID = id,
                    Title = title,
                    Sequence = sequence,
                    IsShow = isShow,
                    CreateTime = DateTime.Now,
                    Url = url,
                    ParentID = parentID
                };

                MenuBiz.Instance.Update(mu);

                return Json(new { code = 1, message = "修改成功" });
            }
            catch (Exception ex)
            {
                return Json(new { code = 0, message = ex.Message });
            }
        }

        public JsonResult Delete(string id)
        {
            try
            {
                bool result = MenuBiz.Instance.Delete(id);
                if (result)
                {
                    return Json(new { code = 1, message = "删除成功" });
                }
                else
                {
                    return Json(new { code = 0, message = "当前菜单有子菜单，删除失败" });
                }

                
            }
            catch (Exception ex)
            {
                return Json(new { code = 0, message = ex.Message });
            }
        }

        public ActionResult SetRoleMenu()
        {
            return View();
        }

        public JsonResult LoadRoleMenu(string roleID)
        {
            return Json(new { });
        }


    }
}
